<template>
  <div class="xtx-city" ref="target">
    <div class="select" @click="toggle" :class="{active:isShow}">
      <span v-if="!fullLocation" class="placeholder" >请选择配送地址</span>
      <span v-else class="value">{{fullLocation}}</span>
      <i class="iconfont icon-angle-down"></i>
    </div>
    <div class="option" v-show='isShow'>
      <template v-if='list.length'>
        <span @click="changeCity(item)" class="ellipsis" v-for="item in clist" :key="item.code">{{item.name}}</span>
      </template>
      <div class="loading" v-else></div>
    </div>
  </div>
</template>

<script>
import { ref, reactive, computed } from 'vue'
// 看官网
import { onClickOutside } from '@vueuse/core'
// 城市下拉列表
import { getCityList } from '@/api/product.js'

export default {
  name: 'XtxCity',
  emits: ['change-city'],
  props: {
    fullLocation: {
      type: String,
      default: ''
    }
  },
  setup (props, { emit }) {
    const isShow = ref(false)
    const list = ref([])

    // 选中的省 市 区
    const changeResult = reactive({
      provinceCode: '',
      provinceName: '',
      cityCode: '',
      cityName: '',
      countyCode: '',
      countyName: '',
      fullLocation: '' // 完整的城市区
    })

    // 计算省市区类别数据
    const clist = computed(() => {
      // 省级列表
      let result = list.value
      // 计算市级列表
      if (changeResult.provinceCode && changeResult.provinceName) {
        // 省级单位已经选中，可以计算市级单位
        result = result.find(item => item.code === changeResult.provinceCode).areaList
      }
      if (changeResult.cityCode && changeResult.cityName) {
        // 选中了市级单位，此时可以获取县级列表
        result = result.find(item => item.code === changeResult.cityCode).areaList
      }
      return result
    })

    const toggle = async () => {
      isShow.value = !isShow.value
      if (isShow.value) {
        // 打开了弹层，调用接口
        const data = await getCityList()
        list.value = data
      }
      // 清空之前选中的数据
      for (const key in changeResult) {
        changeResult[key] = ''
      }
    }
    // 监听下拉弹层之外的区域
    const target = ref(null)
    onClickOutside(target, () => {
      // 如果点击了target元素之外的区域，就自动触发该函数
      isShow.value = false
    })

    // 选中城市：省市区
    const changeCity = (info) => {
      if (info.level === 0) {
        // 省级单位
        changeResult.provinceCode = info.code
        changeResult.provinceName = info.name
      } else if (info.level === 1) {
        // 市级单位
        changeResult.cityCode = info.code
        changeResult.cityName = info.name
      } else {
        // 县级单位
        changeResult.countyCode = info.code
        changeResult.countyName = info.name
        // 点击县级之后就完成了整体的选择
        changeResult.fullLocation = `${changeResult.provinceName} ${changeResult.cityName} ${changeResult.countyName}`
        // 把城市组件中选中的结果最终传递到父组件即可
        emit('change-city', changeResult)
        // = 关闭弹层 数据传回去时关闭弹窗
        isShow.value = false
      }
    }

    return { isShow, toggle, target, list, changeCity, clist }
  }
}
</script>

<style scoped lang="less">
.xtx-city {
  display: inline-block;
  position: relative;
  z-index: 400;
  .select {
    border: 1px solid #e4e4e4;
    height: 30px;
    padding: 0 5px;
    line-height: 28px;
    cursor: pointer;
    &.active {
      background: #fff;
    }
    .placeholder {
      color: #999;
    }
    .value {
      color: #666;
      font-size: 12px;
    }
    i {
      font-size: 12px;
      margin-left: 5px;
    }
  }
  .option {
    width: 542px;
    border: 1px solid #e4e4e4;
    position: absolute;
    left: 0;
    top: 29px;
    background: #fff;
    min-height: 30px;
    line-height: 30px;
    display: flex;
    flex-wrap: wrap;
    padding: 10px;
    // 省略...
    .loading {
      height: 290px;
      width: 100%;
      background: url(../../assets/images/loading.gif) no-repeat center;
    }
    > span {
      width: 130px;
      text-align: center;
      cursor: pointer;
      border-radius: 4px;
      padding: 0 3px;
      &:hover {
        background: #f5f5f5;
      }
    }

  }
}
</style>
